package vn.buffalocoders.rikos.ui.network;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import vn.buffalocoders.rikos.ui.network.CustomMultipartEntity.ProgressListener;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

public class HttpMultipartPost extends AsyncTask<String, Integer, String> {

	private Context context;
	private String filePath;
	private ProgressDialog pd;
	private long totalSize;
	private String requestUrl;
	private String authToken;
	private String userId;
	private String postFileParam;
	private String progressBarTitle;
	
	public HttpMultipartPost(Context context, String filePath, String requestUrl) {
		this.context = context;
		this.filePath = filePath;
		this.requestUrl = requestUrl;
	}
	
	public HttpMultipartPost(Context context, String filePath, String requestUrl, String authToken, String userId, String postFileParam, String progressBarTitle) {
		this.context = context;
		this.filePath = filePath;
		this.requestUrl = requestUrl;
		this.authToken = authToken;
		this.userId = userId;
		this.postFileParam = postFileParam;
		this.progressBarTitle = progressBarTitle;
	}

	@Override
	protected void onPreExecute() {
		pd = new ProgressDialog(context);
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		if(!TextUtils.isEmpty(this.progressBarTitle)) {
			pd.setMessage(progressBarTitle);
		}
		else {
			pd.setMessage("Uploading Picture...");
		}
		pd.setCancelable(false);
		pd.show();
	}

	@Override
	protected String doInBackground(String... params) {
		String serverResponse = null;
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext httpContext = new BasicHttpContext();
		HttpPost httpPost = new HttpPost(requestUrl);

		try {
			CustomMultipartEntity multipartContent = new CustomMultipartEntity(
					new ProgressListener() {
						@Override
						public void transferred(long num) {
							publishProgress((int) ((num / (float) totalSize) * 100));
						}
					});

			// FileBody
			
			if (!TextUtils.isEmpty(postFileParam)) {
				multipartContent.addPart(postFileParam, new FileBody(new File(filePath)));
			}
			else {
				multipartContent.addPart("value", new FileBody(new File(filePath)));
			}
			totalSize = multipartContent.getContentLength();
			if (!TextUtils.isEmpty(authToken) && !TextUtils.isEmpty(userId)) {
				httpPost.setHeader(ConnectRikoServer.X_RIKO_AUTH_TOKEN, authToken);
				httpPost.setHeader(ConnectRikoServer.X_RIKO_USER_ID, userId);
			}
			httpPost.setEntity(multipartContent);
			HttpResponse response = httpClient.execute(httpPost, httpContext);
			serverResponse = EntityUtils.toString(response.getEntity());
			Log.i("LETRUONG", String.format("HttpMultiPart=%s", serverResponse));
			System.out.println(serverResponse);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return serverResponse;
	}

	@Override
	protected void onProgressUpdate(Integer... progress) {
		pd.setProgress((int) (progress[0]));
	}

	@Override
	protected void onPostExecute(String result) {
		System.out.println("result: " + result);
		pd.dismiss();
	}

	@Override
	protected void onCancelled() {
		System.out.println("cancle");
	}
}